<?php

namespace app\admin\model;

use app\admin\model\Data as DataModel;

class DataClasses
{
    public static function countRaningNum($rmap,$ranting=0)
    {
        $count = SportRecord::where($rmap)->where('rating',$ranting)->group('memberId')->count('*');
        return $count;
    }

    //各项目平均分于等级
    //1=男生2=女生
    public static function levelAvg($departId,$gender = 1)
    {
        $list = self::projectSelect();
        $list1 = array_column($list,'name');
        $new_list = [];
        $list2 = [];
        $list3 = [];
        $list4 = [];

        foreach($list as $v){
            $rmap = [];
            $rmap[] = ['projectId','=',$v['id']];
            $rmap[] = ['gender',"=",$gender];
            $rmap[] = [getProfField(input('departmentsId')),"=",$departId];
            $rmap = array_merge_recursive($rmap,whereSchoolMap(false));
            if(getParam('man_level') && $gender == 1){
                $rmap[] = [getClassField(input('departmentsId')),'=',getParam('man_level')];
            }
            if(getParam('woman_level') && $gender == 2){
                $rmap[] = [getClassField(input('departmentsId')),'=',getParam('woman_level')];
            }
            $goodNum = self::countRaningNum($rmap,1);
            $liangNum = self::countRaningNum($rmap,2);
            $jigeNum = self::countRaningNum($rmap,3);
            $noJigeNum = self::countRaningNum($rmap,0);

            $total = $goodNum+$liangNum+$jigeNum+$noJigeNum;

            $goodRate = cacl_rate($goodNum+$liangNum,$total);
            $hegeRate = cacl_rate($goodNum+$liangNum+$jigeNum,$total);
            $avg = SportRecord::where($rmap)->whereNotNull('pingscore')->avg('pingscore');

            $list2[] = (int)$avg;
            $list3[] = (int)$goodRate;
            $list4[] = (int)$hegeRate;


        }
        $data = [$list2,$list3,$list4];
        return [$list1,$data];
    }

    //各院系项目等级
    public static function projectLevel($departId)
    {
        $list = self::getProfList($departId);
        $new_list = [];
        $post = input();
        $list1 = array_column($list,'name');
        $list2 = [];
        $list3 = [];
        $list4 = [];
        $list5 = [];
        foreach($list as $v){
            $rmap = [];
            $rmap[] = [getClassField(input('departmentsId')),'=',$v['id']];
            $rmap = array_merge_recursive($rmap,whereSchoolMap(false));
            $goodNum = self::countRaningNum($rmap,1);
            $liangNum = self::countRaningNum($rmap,2);
            $jigeNum = self::countRaningNum($rmap,3);
            $noJigeNum = self::countRaningNum($rmap,0);

            $total = $goodNum+$liangNum+$jigeNum+$noJigeNum;

            $goodRate = cacl_rate($goodNum,$total);
            $liangRate = cacl_rate($liangNum,$total);
            $jigeRate = cacl_rate($jigeNum,$total);
            $noJigeNum = cacl_rate($noJigeNum,$total);

            $list2[] = (int)$goodRate;
            $list3[] = (int)$liangRate;
            $list4[] = (int)$jigeRate;
            $list5[] = (int)$noJigeNum;
        }
        array_unshift($list1,'product');
        array_unshift($list2,'优秀');
        array_unshift($list3,'良好');
        array_unshift($list4,'合格');
        array_unshift($list5,'不合格');
        $list = [
            $list1, $list2,$list3,$list4,$list5
        ];
        //print_r($list);
        return $list;
    }


    public static function getProfList($id)
    {
        $list = Clss::where('professional',$id)
        ->where('mark',1)
        ->select()
        ->toArray();
        return $list;
    }

    //身高体重BMI
    public static function xueyuanBmi($departId)
    {

        if(getParam('high_weight')){
            $list =  Clss::where('mark',1)
                ->where('id',getParam('high_weight'))
                ->cache(true,130)
                ->select()
                ->toArray();
        }else{
            $list = self::getProfList($departId);
        }
        $list1 = array_column($list,'name');
        $list2 = [];
        $list3 = [];
        foreach($list as $v){
            $map1 = [];
            $map1[] = [getClassField(input('departmentsId')),'=',$v['id']];
            $map1[] = ['itemCode','=','weight'];
            $map1 = array_merge_recursive($map1,whereSchoolMap(false));
            $weight = SportRecord::where($map1)->avg('score');//体重
            $map2 = [];
            $map2[] = [getClassField(input('departmentsId')),'=',$v['id']];
            $map2[] = ['itemCode','=','height'];
            $map2 = array_merge_recursive($map2,whereSchoolMap(false));
            $height = SportRecord::where($map2)->avg('score');//体重

            $list2[] = (int)$weight;
            $list3[] = (int)$height;
        }
        return [$list1,$list2,$list3];
    }

    public static function yuanxiAvg($departId)
    {
        $list = self::getProfList($departId);
        $new_list = [];
        $list1 = array_column($list,'name');
        $list2 = [];
        $list3 = [];
        $list4 = [];
        foreach($list as $v){
            $map = [];
            $map[] = ['gender','=',1];
            $map[] = [getClassField(input('departmentsId')),'=',$v['id']];
            $map = array_merge_recursive($map,whereSchoolMap(false));
            $s1 = (int)SportRecord::where($map)->avg('pingscore');

            $wmap = [];
            $wmap[] = ['gender','=',2];
            $wmap[] = [getClassField(input('departmentsId')),'=',$v['id']];
            $wmap = array_merge_recursive($wmap,whereSchoolMap(false));
            $s2 = (int)SportRecord::where($wmap)->avg('pingscore');

            $amap = [];
            $amap[] = [getClassField(input('departmentsId')),'=',$v['id']];
            $amap += whereSchoolMap();
            $amap = array_merge_recursive($amap,whereSchoolMap(false));
            $avg = SportRecord::where($amap)->avg('pingscore');

            $list2[] = pro_float($s1);
            $list3[] = pro_float($s2);
            $list4[] = pro_float($avg);
        }
        array_unshift($list1,'product');
        array_unshift($list2,'男生');
        array_unshift($list3,'女生');
        array_unshift($list4,'平均分');
        $list = [
            $list1, $list2,$list3,$list4
        ];
        return $list;
    }

    public static function ydshichang($departId)
    {
        $list = self::projectSelect();

        $new_list = [];
        //参与率计算
        foreach($list as $k=>$v){
            $new_list[$k]['time'] = $v['name'];
            $map = [];
            $map[] = ['gender' ,'=', 1];
            $map[] = ['projectId' ,'=', $v['id']];
            $map[] = [getProfField(input('departmentsId')) ,'=',$departId];
            if(getParam('yundongshichang')){
                $map[] = [getProfField(input('departmentsId')),'=',getParam('yundongshichang')];
            }
            $map = array_merge_recursive($map,whereSchoolMap(false));
            $man_count1 = SportRecord::where($map)->whereNotNull('duration')->sum('duration');
            $man_count2 = SportRecord::where($map)->where('unit','秒')->sum('score');
            $man_count = $man_count1+$man_count2;

            $map = [
                'gender' => 2,
                'projectId' => $v['id'],
                getProfField(input('departmentsId')) => $departId,
            ];
            if(getParam('yundongshichang')){
                $map += [getProfField(input('departmentsId'))=>getParam('yundongshichang')];
            }
            $woman_count1 =  SportRecord::where($map)->whereNotNull('duration')->sum('duration');
            $woman_count2 = SportRecord::where($map)->where('unit','秒')->sum('score');
            $woman_count = $woman_count1+$woman_count2;
            $new_list[$k]['views'] = $man_count;
            $new_list[$k]['visits'] = $woman_count;
        }
        //print_r($new_list);die();
        return $new_list;
    }

    //体测人数
    public static function ticeren($map = "")
    {
        $count = SportRecord::where($map)->group('memberId')->count();
        //echo $count;
        return $count;
    }

    public static function projectSelect()
    {
        $list = Data::projectSelect();
        return $list;
    }

    public static function projectArrList($departId)
    {
        $list = self::projectSelect();
        //print_r($list);
        //项目人数/参与人数 = 参与率
        //echo input('departmentsId');
        $total = Memb::where(getMembProfField(input('departmentsId')),$departId)->count();//参与体测总人数
        $rate = [];
        //参与率计算
        foreach($list as $v){
            if($total){
                $smap = [];
                $smap[] = ['projectId','=',$v['id']];
                $smap[] = [getProfField(input('departmentsId')),'=',$departId];
                //院系画像筛选
                if(getParam('canyulv')){
                    $smap[] = [getClassField(input('departmentsId')),'=',getParam('canyulv')];
                }
                $smap  = array_merge_recursive($smap,whereSchoolMap(false));
                //print_r($smap);
                $pro_num = self::ticeren($smap);
                //echo $pro_num.PHP_EOL;
                $res = (int)cacl_rate($pro_num,$total);
            }else{
                $res = 0;
            }
            $rate[] = $res;
        }
        $list = array_column($list,'name');
        //print_r($list);
        return [$list,$rate];
    }

    public static function xueyuanArrList($departId)
    {
        $list = Clss::where('mark',1)
            ->where('professional',$departId)
            ->cache(true,60)
            ->select()
            ->toArray();
        $new_list =[];

        $data1 = [];
        $data2 = [];
        $data3 = [];
        $data4 = [];

        foreach($list as $v){
            $new_list[]=$v['name'];
            $map = [];
            $map[] = [getClassField(input('departmentsId')),'=',$v['id']];
            $map = array_merge_recursive($map,whereSchoolMap(false));
            $c1 = self::ranting($map,0);//不及格
            $c2 =self::ranting($map,1);//优秀
            $c3 = self::ranting($map,2);//良好
            $c4 = self::ranting($map,3);//及格
            $sum = $c1+$c2+$c3+$c4;
            //echo $c1.'-'.$c2.'-'.$c3.'-'.$c4.'-'.$sum.PHP_EOL;
            $data1[] = cacl_rate($c1,$sum).'%';
            $data2[] = cacl_rate($c2,$sum).'%';
            $data3[] = cacl_rate($c3,$sum).'%';
            $data4[] = cacl_rate($c4,$sum).'%';


        }
        $rate_list =  [
            [
                'name' => '优秀',
                'type'=>'bar',
                'stack' =>'Search Engine',
                'emphasis'=>['focus','series'],
                'label'=>'this.labelOption',
                'data'=> $data1
            ],
            [
                'name' => '良好',
                'type'=>'bar',
                'stack' =>'Search Engine',
                'emphasis'=>['focus','series'],
                'label'=>'this.labelOption',
                'data'=> $data2
            ],
            [
                'name' => '合格',
                'type'=>'bar',
                'stack' =>'Search Engine',
                'emphasis'=>['focus','series'],
                'label'=>'this.labelOption',
                'data'=> $data3
            ],
            [
                'name' => '不合格',
                'type'=>'bar',
                'stack' =>'Search Engine',
                'emphasis'=>['focus','series'],
                'label'=>'this.labelOption',
                'data'=> $data4
            ],
        ];
        //print_R($new_list);die();
        return [$new_list,$rate_list];
    }
    public static function ranting($map,$raning)
    {
        $map[] = ['rating' ,'=',$raning];
        $c1 = SportRecord::where($map)->whereNotNull('pingscore')->count('rating');//不及格
        return $c1;
    }

    public static function countBmi($map,$gender = '')
    {
        $map = array_merge_recursive($map,whereSchoolMap(false));
        if($gender){
            $map[] = ['gender' , '=' , $gender];
        }
        if(getParam('high_weight')){
            //根据表格结果需要切换筛选字段
            $map[] = ['classId', '=',getParam('high_weight')];
        }
        $count = SportRecord::where($map)
            ->group('memberId')
            //->whereNull('bmi')
            ->count();
        return $count;
    }

    public static function selectBmi($departId,$gender = '')
    {
        $row = Standard::where('name','BMI指数')->find();
        if($row){
            /* $sta = $row['standard'];
             print_r($sta);*/
            //男 偏瘦 小于 17.9 正常17.9-24 超重24 - 28 肥胖>28
            $ps = self::countBmi([['bmi','<',17.9],[getProfField(input('departmentsId')),'=',$departId]],$gender);
            $zc = self::countBmi([['bmi',['>=',17.9],['<=',24]],[getProfField(input('departmentsId')),'=',$departId]],$gender);
            $cz = self::countBmi([['bmi',['>',24],['<=',28]],[getProfField(input('departmentsId')),'=',$departId]],$gender);
            $fp = self::countBmi([['bmi','>',28],[getProfField(input('departmentsId')),'=',$departId]],$gender);

            // 女 偏瘦 小于17.9 正常17.9-24 超重24 - 28  肥胖>28
            $total = $ps+$zc+$cz+$fp;
            //echo $total;
            $rate1 = 0;
            $rate2 = 0;
            $rate3 = 0;
            $rate4 = 0;
            if($total>0){
                $rate1 = cacl_rate($ps,$total);
                $rate2 = cacl_rate($zc,$total);
                $rate3 = cacl_rate($cz,$total);
                $rate4 = cacl_rate($fp,$total);
            }
            $res = [
                [ 'value'=> $rate1, 'name'=>'偏瘦',],
                [ 'value'=> $rate2, 'name'=>'正常',],
                [ 'value'=> $rate3, 'name'=>'过量',],
                [ 'value'=> $rate4, 'name'=>'肥胖',]
            ];
            return $res;
        }else{
            $res = [
                [ 'value'=> 0, 'name'=>'偏瘦',],
                [ 'value'=> 0, 'name'=>'正常',],
                [ 'value'=> 0, 'name'=>'过量',],
                [ 'value'=> 0, 'name'=>'肥胖',]
            ];
            return $res;
        }
    }

    public static function duanpao($memb)
    {
        $map = [];
        $map[] = ['memberId','=',$memb['id']];
        $projectId = 12;
        if(getParam('duanpao')){
            $projectId = getParam('duanpao');
        }
        $map[] = ['projectId','=',$projectId];
        $map = array_merge_recursive($map,whereSchoolMap(false));
        $sum = SportRecord::where($map)->max('score');
        $goodScore = SportRecord::where($map)->max('pingscore');
        $where1 = [];
        $where2 = [];
        $where3 = [];
        //班级排名
        $where1[] = ['memberId','=',$memb['id']];
        $where1[] = ['projectId','=',$projectId];

        $where2[] = ['classId','=',$memb['class']];
        $where2[] = ['projectId','=',$projectId];

        $classRank = DataModel::sutrank($where1,$where2);

        $where3[] = ['gradeId', '=', $memb['grade']];
        $where3[] = ['projectId', '=', $projectId];
        $gradeRank =  DataModel::sutrank($where1,$where3);

        $rate1 = 0;
        $rate2 = 0;
        $rate3 = 0;
        $rate4 = 0;
        $yx = SportRecord::where($where1)->where('rating',1)->count();
        $lh = SportRecord::where($where1)->where('rating',2)->count();
        $jg = SportRecord::where($where1)->where('rating',3)->count();
        $nojg = SportRecord::where($where1)->where('rating',4)->count();
        $total = $yx+$lh+$jg+$nojg;
        if($total>0){
            $rate1 = cacl_rate($yx,$total);
            $rate2 = cacl_rate($lh,$total);
            $rate3 = cacl_rate($jg,$total);
            $rate4 = cacl_rate($nojg,$total);
        }
       $res = [
            'good' => $sum.'秒',
            'good_score' => $goodScore,
            'grade_rank' =>$gradeRank,
            'class_rank' =>$classRank,
            'data'=> [
                [ 'value'=> $rate1, 'name'=>'优秀',],
                [ 'value'=> $rate2, 'name'=>'良好',],
                [ 'value'=> $rate3, 'name'=>'合格',],
                [ 'value'=> $rate4, 'name'=>'不合格',],
            ],
        ];
       return $res;
    }

    public static function tiaosheng($memb)
    {
        $map = [];
        $map[] = ['memberId','=',$memb['id']];
        $projectId = 13;
        if(getParam('tiaosheng')){
            $projectId = getParam('tiaosheng');
        }
        $map[] = ['projectId','=',$projectId];
        $map = array_merge_recursive($map,whereSchoolMap(false));
        $max = SportRecord::where($map)->field('max(score) as max_score,unit')->find();
        $goodScore = SportRecord::where($map)->max('pingscore');
        $where1 = [];
        $where2 = [];
        $where3 = [];
        //班级排名
        $where1[] = ['memberId','=',$memb['id']];
        $where1[] = ['projectId','=',$projectId];

        $where2[] = ['classId','=',$memb['class']];
        $where2[] = ['projectId','=',$projectId];

        $classRank = DataModel::sutrank($where1,$where2);

        $where3[] = ['gradeId', '=', $memb['grade']];
        $where3[] = ['projectId', '=', $projectId];
        $gradeRank =  DataModel::sutrank($where1,$where3);

        $rate1 = 0;
        $rate2 = 0;
        $rate3 = 0;
        $rate4 = 0;
        $yx = SportRecord::where($where1)->where('rating',1)->count();
        $lh = SportRecord::where($where1)->where('rating',2)->count();
        $jg = SportRecord::where($where1)->where('rating',3)->count();
        $nojg = SportRecord::where($where1)->where('rating',4)->count();
        $total = $yx+$lh+$jg+$nojg;
        if($total>0){
            $rate1 = cacl_rate($yx,$total);
            $rate2 = cacl_rate($lh,$total);
            $rate3 = cacl_rate($jg,$total);
            $rate4 = cacl_rate($nojg,$total);
        }
        $res = [
            'good' => $max?$max['max_score'].$max['unit']:'',
            'good_score' => $goodScore,
            'grade_rank' =>$gradeRank,
            'class_rank' =>$classRank,
            'data'=> [
                [ 'value'=> $rate1, 'name'=>'优秀',],
                [ 'value'=> $rate2, 'name'=>'良好',],
                [ 'value'=> $rate3, 'name'=>'合格',],
                [ 'value'=> $rate4, 'name'=>'不合格',],
            ],
        ];
        return $res;
    }

    public static function stufeihuo($memb)
    {
        $map = [];
        $map[] = ['memberId','=',$memb['id']];
        $projectId = 8;

        $map[] = ['projectId','=',$projectId];
       //$map[] = ['itemCode','=','vitalCapac'];
        $map = array_merge_recursive($map,whereSchoolMap(false));
        $max = SportRecord::where($map)->field('max(score) as max_score,unit')->find();
        $goodScore = SportRecord::where($map)->max('pingscore');
        $where1 = [];
        $where2 = [];
        $where3 = [];
        //班级排名
        $where1[] = ['memberId','=',$memb['id']];
        $where1[] = ['projectId','=',$projectId];

        $where2[] = ['classId','=',$memb['class']];
        $where2[] = ['projectId','=',$projectId];

        $classRank = DataModel::sutrank($where1,$where2);

        $where3[] = ['gradeId', '=', $memb['grade']];
        $where3[] = ['projectId', '=', $projectId];
        $gradeRank =  DataModel::sutrank($where1,$where3);

        $res = [
            'good' => $max?$max['max_score'].$max['unit']:'',
            'good_score' => $goodScore,
            'grade_rank' =>$gradeRank,
            'class_rank' =>$classRank,
            'data'=> self::feihuo($memb['id'],$projectId),
        ];
        return $res;
    }

    public static function feihuoCount($userId,$projectId,$map1,$gender = ''){
        $map=[];
        $map[] = ['projectId','=',$projectId];
        $map[] = ['memberId','=',$userId];
        $map[] = ['itemCode','=','vitalCapac'];
        $res = self::countBmi(array_merge_recursive($map,$map1),$gender);
        return $res;
    }

    public static function feihuo($userId,$projectId)
    {
        $yx1 = self::feihuoCount($userId,$projectId,[['score','>=',4800]],1);
        $lh1 = self::feihuoCount($userId,$projectId,[['score',['>=',4300],['<',4800]]],1);
        $jg1 = self::feihuoCount($userId,$projectId,[['score',['>=',3100],['<',4300]]],1);
        $nojg1 = self::feihuoCount($userId,$projectId,[['score','<',3100]],1);
        $yx2 = self::feihuoCount($userId,$projectId,[['score','>=',3200]],2);
        $lh2 = self::feihuoCount($userId,$projectId,[['score',['>=',3000],['<',3200]]],2);
        $jg2 = self::feihuoCount($userId,$projectId,[['score',['>=',2000],['<',3000]]],2);
        $nojg2 = self::feihuoCount($userId,$projectId,[['score','<',2000]],2);
        $total = $yx1+$lh1+$jg1+$nojg1+$yx2+$lh2+$jg2+$nojg2;
        $rate1 = 0;
        $rate2 = 0;
        $rate3 = 0;
        $rate4 = 0;
        if($total>0){
            $rate1 = cacl_rate($yx1+$yx2,$total);
            $rate2 = cacl_rate($lh1+$lh2,$total);
            $rate3 = cacl_rate($jg1+$jg2,$total);
            $rate4 = cacl_rate($nojg1+$nojg2,$total);
        }
        $res = [
            [ 'value'=> $rate1, 'name'=>'优秀',],
            [ 'value'=> $rate2, 'name'=>'良好',],
            [ 'value'=> $rate3, 'name'=>'及格',],
            [ 'value'=> $rate4, 'name'=>'不及格',]
        ];
        return $res;
    }
}